import { isReserveTag } from './util';
import {vnode} from './vnode/index';
// vm, tag, attrs, children, key, text, componentOptions
export function renderMixin(Vue) {
    Vue.prototype._c = function (tag, attrs={}, ...children) {
        const vm = this;
        if (isReserveTag(tag)) {
            return vnode(vm, tag, attrs, children, attrs.key, undefined);
        } else {
            return createComponent(vm, tag, attrs, undefined, attrs.key, undefined);
        }
    }
    Vue.prototype._v = function (text) {
        return vnode(this, undefined, undefined, undefined, undefined, text);
    }
    Vue.prototype._s = function (text) {
        if (text) {
            return typeof text === 'object' ? JSON.stringify(text) : text;
        }
    }
}

function createComponent(vm, tag, attrs, children, key, text) {
    // 1获取到组件的配置
    let Ctor = vm.$options.components[tag];
    if (typeof Ctor === 'object') {
        Ctor = vm.constructor.options._base.extend(Ctor);
    }
    attrs.hook = {
        init(vnode) {
            // 获取构造函数
            vnode.componentInstance = new vnode.componentOptions.Ctor({}).$mount();
        }
    }
    // vm, tag, attrs, children, key, text, componentOptions
    // 比较重要的: attrs.hook {Ctor}
    return vnode(vm, `Vue-components-${Ctor.cid}-${tag}`, attrs, undefined, key, undefined, {
        Ctor
    });
}


// $vnode是组件的vnode 名字一般是 vue-components-xxx-xxx 
// _vnode是组件内的真正标签的vnode

// createComponentInstance 为组件的_parentVnode赋值了组件的vnode